import os
import json
class Save_as_Json(object):
    def __init__(self,path,item) -> None:
        self.path = path
        self.item = item
        
    def save_file(self):
        
        # 将字典对象转化为可写入文本的字符串
        item = json.dumps(self.item)
        try:
            if not os.path.exists(self.path):
                with open(self.path, "w", encoding='utf-8') as f:
                    f.write(item + "\n")
                    print("数据写入成功!")
            else:
                with open(self.path, "a", encoding='utf-8') as f:
                    f.write(item + ",\n")
                    print("数据写入成功!")
        except Exception as e:
            print("数据写入失败!", e)
        